package com.acegear.horizon.domain.events.handlers;

import com.acegear.horizon.domain.events.OrderCreatedEvent;
import com.acegear.horizon.domain.events.OrderPayedEvent;
import com.acegear.horizon.domain.events.OrderTimeoutEvent;
import com.acegear.horizon.domain.models.constraint.ClubMemberType;
import com.acegear.horizon.domain.models.jpa.ClubEventMember;
import com.acegear.horizon.domain.models.jpa.UserInfo;
import com.acegear.horizon.domain.repository.ClubEventRepository;
import com.acegear.horizon.domain.repository.ClubRepository;
import com.acegear.horizon.domain.repository.jpa.ClubEventMemberRepository;
import com.acegear.horizon.domain.repository.jpa.EventOrderRepository;
import com.acegear.horizon.domain.repository.jpa.UserInfoRepository;
import com.acegear.horizon.vendor.ons.ONSConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * Created by wangsike on 2017/2/24.
 */
@Component
@Transactional
public class OrderEventHandler {

    @Autowired
    private EventOrderRepository eventOrderRepository;

    @Autowired
    private ClubEventMemberRepository clubEventMemberRepository;

    @Autowired
    private ClubEventRepository clubEventRepository;

    @Autowired
    private ClubRepository clubRepository;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private ONSConsumer consumer;

    @PostConstruct
    public void handleOrderPayONS() {
        consumer.consume(OrderPayedEvent.class, this::handleOrderPay);
    }

    @PostConstruct
    public void handeOrderCreatedONS() {
        consumer.consume(OrderCreatedEvent.class, this::handeOrderCreated);
    }

    @PostConstruct
    public void handleOrderTimeoutONS() {
        consumer.consume(OrderTimeoutEvent.class, this::handleOrderTimeout);
    }

    public void handleOrderPay(OrderPayedEvent orderPayedEvent) {
        eventOrderRepository
                .findByOrderId(orderPayedEvent.getOrderId())
                .ifPresent(eventOrder -> {
                    UserInfo userInfo = userInfoRepository.findByUserId(eventOrder.getUserId()).get();
                    if (!clubEventMemberRepository.findByEventIdAndUserId(eventOrder.getEventId(), eventOrder.getUserId()).isPresent()) {
                        ClubEventMember clubEventMember = new ClubEventMember(
                                eventOrder.getEventId(),
                                eventOrder.getUserId(),
                                userInfo.getAvatar()
                        );
                        clubEventMemberRepository.save(clubEventMember);
                        clubEventRepository.findByEventId(eventOrder.getEventId())
                                .flatMap(event -> clubRepository.findByClubId(event.getEventBase().getOwnerId()))
                                .ifPresent(club -> {
                                    club.addAndSaveMember(userInfo, ClubMemberType.MEMBER);
                                });
                    }
                });
    }

    public void handeOrderCreated(OrderCreatedEvent orderCreatedEvent) {
    }

    public void handleOrderTimeout(OrderTimeoutEvent orderTimeoutEvent) {

    }
}
